<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <div class="form-item">
          <span class="title">创建日期</span>
          <el-date-picker
            v-model="createdateList"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          ></el-date-picker>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item">
          <span class="title" style="width:80px">分店名称</span>
          <el-select v-model="param.data.storeId" placeholder="请选择">
            <el-option
              v-for="item in productOptions"
              :key="item.toreId"
              :label="item.name"
              :value="item.toreId"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item button-container">
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" @click="resetSearchCotnet" icon="el-icon-refresh">重置</el-button>
        </div>
      </el-col>
    </el-row>
    <div class="table-body">
      <el-table :data="tableData" border v-loading="isLoading" style="width: 100%">
        <el-table-column prop="name" label="分店名称"></el-table-column>
        <el-table-column prop="purchaseTime" label="进货时间"></el-table-column>
        <el-table-column prop="title" label="商品名称"></el-table-column>
        <el-table-column prop="purchaseNum" label="进货数量"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="handleToDetial(scope.row)"
              size="small"
              v-if="isPermissionsBtn('goGoodApproval_agree')"
            >同意</el-button>
            <el-button
              style="margin-left:15px"
              size="small"
              type="danger"
              @click="handleRefuse(scope.row)"
              v-if="isPermissionsBtn('goGoodApproval_refuse')"
            >拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="currentPageChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
  name: "",
  data() {
    return {
      param: {
        data: {
          storeId: "",
          startCreateTime: "",
          endCreateTime: ""
        },
        queryRequest: {
          pageNum: 1,
          pageSize: 10
        }
      },
      createdateList: [],
      isLoading: false,
      tableData: [],
      total: 1,
      productOptions: []
    };
  },
  components: {},
  computed: {
    ...mapGetters(["isPermissionsBtn"])
  },
  beforeMount() {},
  mounted() {
    this.initData();
    this.getStoreList();
  },
  methods: {
    initData() {
      this.isLoading = true;
      if (this.createdateList.length > 0) {
        this.param.data.startCreateTime = this.createdateList[0];
        this.param.data.endCreateTime = this.createdateList[1];
      }
      this.$service.tInventoryrecordsSelectStatusApp(this.param).then(res => {
        this.isLoading = false;
        if (res.code == "00000") {
          if (
            res.data.rows.length == 0 &&
            this.param.queryRequest.pageNum != 1
          ) {
            this.param.queryRequest.pageNum -= 1;
            this.initData();
          }
          this.tableData = res.data.rows || [];
          this.total = res.data.total || 0;
        }
      });
    },
    getStoreList() {
      this.$service.userGetStoreList().then(res => {
        if (res.code == "00000") {
          this.productOptions = res.data || [];
        }
      });
    },
    search() {
      this.param.queryRequest.pageNum = 1;
      this.initData();
    },
    resetSearchCotnet() {
      //重置搜索条件
      this.param.data.storeId = "";
      this.param.data.startCreateTime = "";
      this.param.data.endCreateTime = "";
      this.createdateList = [];
    },
    handleToDetial(scope) {
      this.$confirm("是否同意审批?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.agreeData(scope.id);
      });
    },
    agreeData(id) {
      var param = {
        id: id,
        approvalStatus: "1"
      };
      this.$service.tInventoryrecordsUpdateStatus(param).then(res => {
        if (res.code == "00000") {
          this.$message({
            message: "同意审批成功",
            type: "success"
          });
          this.initData();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
    handleRefuse(scope) {
      this.$confirm("是否拒绝审批?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.refuseData(scope.id);
      });
    },
    refuseData(id) {
      var param = {
        id: id,
        approvalStatus: "2"
      };
      this.$service.tInventoryrecordsUpdateStatus(param).then(res => {
        if (res.code == "00000") {
          this.$message({
            message: "拒绝审批成功",
            type: "success"
          });
          this.initData();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
    currentPageChange(e) {
      this.param.queryRequest.pageNum = e;
      this.initData();
    }
  },
  watch: {}
};
</script>
<style scoped>
</style>
